/* ============================================================================
Copyright (c) 2008-2014, Broadcom Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
============================================================================ */

#include "interface/khronos/common/khrn_int_common.h"

#include "interface/khronos/common/khrn_int_color.h"
#include "interface/khronos/common/khrn_int_math.h"

const uint8_t COLOR_S_TO_LIN[256] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
   0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
   0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
   0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04,
   0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
   0x05, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07,
   0x08, 0x08, 0x08, 0x08, 0x09, 0x09, 0x09, 0x0a,
   0x0a, 0x0a, 0x0b, 0x0b, 0x0c, 0x0c, 0x0c, 0x0d,
   0x0d, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x10, 0x10,
   0x11, 0x11, 0x12, 0x12, 0x12, 0x13, 0x13, 0x14,
   0x15, 0x15, 0x16, 0x16, 0x17, 0x17, 0x18, 0x18,
   0x19, 0x1a, 0x1a, 0x1b, 0x1b, 0x1c, 0x1d, 0x1d,
   0x1e, 0x1f, 0x1f, 0x20, 0x21, 0x21, 0x22, 0x23,
   0x23, 0x24, 0x25, 0x26, 0x26, 0x27, 0x28, 0x29,
   0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2d, 0x2e, 0x2f,
   0x30, 0x31, 0x32, 0x33, 0x33, 0x34, 0x35, 0x36,
   0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
   0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
   0x47, 0x48, 0x49, 0x4a, 0x4c, 0x4d, 0x4e, 0x4f,
   0x50, 0x51, 0x52, 0x54, 0x55, 0x56, 0x57, 0x58,
   0x5a, 0x5b, 0x5c, 0x5d, 0x5f, 0x60, 0x61, 0x63,
   0x64, 0x65, 0x67, 0x68, 0x69, 0x6b, 0x6c, 0x6d,
   0x6f, 0x70, 0x72, 0x73, 0x74, 0x76, 0x77, 0x79,
   0x7a, 0x7c, 0x7d, 0x7f, 0x80, 0x82, 0x83, 0x85,
   0x86, 0x88, 0x8a, 0x8b, 0x8d, 0x8e, 0x90, 0x92,
   0x93, 0x95, 0x97, 0x98, 0x9a, 0x9c, 0x9d, 0x9f,
   0xa1, 0xa3, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xad,
   0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb8, 0xba, 0xbc,
   0xbe, 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc,
   0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc,
   0xde, 0xe0, 0xe2, 0xe5, 0xe7, 0xe9, 0xeb, 0xed,
   0xef, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfd, 0xff };

const uint16_t COLOR_S_TO_LIN16[256] = {
   0x0000, 0x0014, 0x0028, 0x003c, 0x0050, 0x0063, 0x0077, 0x008b,
   0x009f, 0x00b3, 0x00c7, 0x00de, 0x00f4, 0x010b, 0x0123, 0x013d,
   0x0157, 0x0173, 0x0190, 0x01af, 0x01cf, 0x01f0, 0x0212, 0x0236,
   0x025c, 0x0282, 0x02aa, 0x02d4, 0x02ff, 0x032b, 0x0359, 0x0388,
   0x03b9, 0x03eb, 0x041f, 0x0454, 0x048b, 0x04c3, 0x04fd, 0x0539,
   0x0576, 0x05b5, 0x05f5, 0x0637, 0x067b, 0x06c0, 0x0707, 0x0750,
   0x079a, 0x07e6, 0x0833, 0x0883, 0x08d4, 0x0927, 0x097b, 0x09d2,
   0x0a2a, 0x0a84, 0x0adf, 0x0b3d, 0x0b9c, 0x0bfd, 0x0c60, 0x0cc5,
   0x0d2b, 0x0d94, 0x0dfe, 0x0e6a, 0x0ed8, 0x0f48, 0x0fba, 0x102e,
   0x10a3, 0x111b, 0x1194, 0x1210, 0x128d, 0x130d, 0x138e, 0x1411,
   0x1497, 0x151e, 0x15a7, 0x1633, 0x16c0, 0x174f, 0x17e1, 0x1874,
   0x190a, 0x19a1, 0x1a3b, 0x1ad7, 0x1b74, 0x1c14, 0x1cb6, 0x1d5b,
   0x1e01, 0x1ea9, 0x1f54, 0x2000, 0x20af, 0x2160, 0x2213, 0x22c8,
   0x2380, 0x243a, 0x24f5, 0x25b4, 0x2674, 0x2736, 0x27fb, 0x28c2,
   0x298b, 0x2a56, 0x2b24, 0x2bf4, 0x2cc6, 0x2d9b, 0x2e71, 0x2f4a,
   0x3026, 0x3103, 0x31e3, 0x32c6, 0x33aa, 0x3491, 0x357a, 0x3666,
   0x3754, 0x3844, 0x3937, 0x3a2c, 0x3b23, 0x3c1d, 0x3d19, 0x3e17,
   0x3f18, 0x401c, 0x4121, 0x422a, 0x4334, 0x4441, 0x4551, 0x4662,
   0x4777, 0x488e, 0x49a7, 0x4ac3, 0x4be1, 0x4d02, 0x4e25, 0x4f4a,
   0x5073, 0x519d, 0x52cb, 0x53fa, 0x552d, 0x5661, 0x5799, 0x58d2,
   0x5a0f, 0x5b4e, 0x5c8f, 0x5dd3, 0x5f1a, 0x6063, 0x61af, 0x62fd,
   0x644e, 0x65a2, 0x66f8, 0x6851, 0x69ac, 0x6b0a, 0x6c6b, 0x6dce,
   0x6f34, 0x709d, 0x7208, 0x7376, 0x74e7, 0x765a, 0x77d0, 0x7948,
   0x7ac4, 0x7c42, 0x7dc2, 0x7f46, 0x80cc, 0x8254, 0x83e0, 0x856e,
   0x86ff, 0x8893, 0x8a29, 0x8bc2, 0x8d5e, 0x8efd, 0x909e, 0x9242,
   0x93e9, 0x9593, 0x973f, 0x98ee, 0x9aa0, 0x9c55, 0x9e0d, 0x9fc7,
   0xa185, 0xa345, 0xa508, 0xa6cd, 0xa896, 0xaa61, 0xac2f, 0xae00,
   0xafd4, 0xb1ab, 0xb385, 0xb561, 0xb741, 0xb923, 0xbb08, 0xbcf0,
   0xbedb, 0xc0c9, 0xc2b9, 0xc4ad, 0xc6a3, 0xc89d, 0xca99, 0xcc98,
   0xce9a, 0xd09f, 0xd2a7, 0xd4b2, 0xd6c0, 0xd8d1, 0xdae5, 0xdcfc,
   0xdf16, 0xe132, 0xe352, 0xe575, 0xe79a, 0xe9c3, 0xebef, 0xee1d,
   0xf04f, 0xf284, 0xf4bb, 0xf6f6, 0xf934, 0xfb74, 0xfdb8, 0xffff };

const uint8_t COLOR_LIN_TO_S[256] = {
   0x00, 0x0d, 0x16, 0x1c, 0x21, 0x26, 0x2a, 0x2e,
   0x31, 0x35, 0x38, 0x3a, 0x3d, 0x40, 0x42, 0x44,
   0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55,
   0x56, 0x58, 0x5a, 0x5b, 0x5d, 0x5f, 0x60, 0x62,
   0x63, 0x65, 0x66, 0x67, 0x69, 0x6a, 0x6c, 0x6d,
   0x6e, 0x70, 0x71, 0x72, 0x73, 0x74, 0x76, 0x77,
   0x78, 0x79, 0x7a, 0x7b, 0x7d, 0x7e, 0x7f, 0x80,
   0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
   0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90,
   0x91, 0x92, 0x93, 0x93, 0x94, 0x95, 0x96, 0x97,
   0x98, 0x99, 0x9a, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
   0x9f, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa3, 0xa4,
   0xa5, 0xa6, 0xa7, 0xa7, 0xa8, 0xa9, 0xaa, 0xaa,
   0xab, 0xac, 0xad, 0xad, 0xae, 0xaf, 0xaf, 0xb0,
   0xb1, 0xb2, 0xb2, 0xb3, 0xb4, 0xb4, 0xb5, 0xb6,
   0xb6, 0xb7, 0xb8, 0xb8, 0xb9, 0xba, 0xba, 0xbb,
   0xbc, 0xbc, 0xbd, 0xbe, 0xbe, 0xbf, 0xc0, 0xc0,
   0xc1, 0xc2, 0xc2, 0xc3, 0xc3, 0xc4, 0xc5, 0xc5,
   0xc6, 0xc7, 0xc7, 0xc8, 0xc8, 0xc9, 0xca, 0xca,
   0xcb, 0xcb, 0xcc, 0xcd, 0xcd, 0xce, 0xce, 0xcf,
   0xcf, 0xd0, 0xd1, 0xd1, 0xd2, 0xd2, 0xd3, 0xd3,
   0xd4, 0xd5, 0xd5, 0xd6, 0xd6, 0xd7, 0xd7, 0xd8,
   0xd8, 0xd9, 0xda, 0xda, 0xdb, 0xdb, 0xdc, 0xdc,
   0xdd, 0xdd, 0xde, 0xde, 0xdf, 0xdf, 0xe0, 0xe0,
   0xe1, 0xe2, 0xe2, 0xe3, 0xe3, 0xe4, 0xe4, 0xe5,
   0xe5, 0xe6, 0xe6, 0xe7, 0xe7, 0xe8, 0xe8, 0xe9,
   0xe9, 0xea, 0xea, 0xeb, 0xeb, 0xec, 0xec, 0xed,
   0xed, 0xed, 0xee, 0xee, 0xef, 0xef, 0xf0, 0xf0,
   0xf1, 0xf1, 0xf2, 0xf2, 0xf3, 0xf3, 0xf4, 0xf4,
   0xf5, 0xf5, 0xf6, 0xf6, 0xf6, 0xf7, 0xf7, 0xf8,
   0xf8, 0xf9, 0xf9, 0xfa, 0xfa, 0xfb, 0xfb, 0xfb,
   0xfc, 0xfc, 0xfd, 0xfd, 0xfe, 0xfe, 0xff, 0xff };

const uint8_t COLOR_LIN16_TO_S[320] = {
   /* 12-bit */
   0x00, 0x01, 0x02, 0x02, 0x03, 0x04, 0x05, 0x06,
   0x06, 0x07, 0x08, 0x09, 0x0a, 0x0a, 0x0b, 0x0c,
   0x0d, 0x0d, 0x0e, 0x0f, 0x0f, 0x10, 0x10, 0x11,
   0x11, 0x12, 0x13, 0x13, 0x14, 0x14, 0x15, 0x15,
   /* 11-bit */
   0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1b,
   0x1c, 0x1d, 0x1d, 0x1e, 0x1f, 0x20, 0x20, 0x21,
   /* 10-bit */
   0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
   0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31,
   /* 9-bit */
   0x31, 0x33, 0x35, 0x36, 0x38, 0x39, 0x3a, 0x3c,
   0x3d, 0x3e, 0x40, 0x41, 0x42, 0x43, 0x44, 0x46,
   /* 8-bit */
   0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55,
   0x56, 0x58, 0x5a, 0x5b, 0x5d, 0x5f, 0x60, 0x62,
   0x63, 0x65, 0x66, 0x67, 0x69, 0x6a, 0x6c, 0x6d,
   0x6e, 0x70, 0x71, 0x72, 0x73, 0x74, 0x76, 0x77,
   0x78, 0x79, 0x7a, 0x7b, 0x7d, 0x7e, 0x7f, 0x80,
   0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
   0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90,
   0x91, 0x92, 0x93, 0x93, 0x94, 0x95, 0x96, 0x97,
   0x98, 0x99, 0x9a, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
   0x9f, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa3, 0xa4,
   0xa5, 0xa6, 0xa7, 0xa7, 0xa8, 0xa9, 0xaa, 0xaa,
   0xab, 0xac, 0xad, 0xad, 0xae, 0xaf, 0xaf, 0xb0,
   0xb1, 0xb2, 0xb2, 0xb3, 0xb4, 0xb4, 0xb5, 0xb6,
   0xb6, 0xb7, 0xb8, 0xb8, 0xb9, 0xba, 0xba, 0xbb,
   0xbc, 0xbc, 0xbd, 0xbe, 0xbe, 0xbf, 0xc0, 0xc0,
   0xc1, 0xc2, 0xc2, 0xc3, 0xc3, 0xc4, 0xc5, 0xc5,
   0xc6, 0xc7, 0xc7, 0xc8, 0xc8, 0xc9, 0xca, 0xca,
   0xcb, 0xcb, 0xcc, 0xcd, 0xcd, 0xce, 0xce, 0xcf,
   0xcf, 0xd0, 0xd1, 0xd1, 0xd2, 0xd2, 0xd3, 0xd3,
   0xd4, 0xd5, 0xd5, 0xd6, 0xd6, 0xd7, 0xd7, 0xd8,
   0xd8, 0xd9, 0xda, 0xda, 0xdb, 0xdb, 0xdc, 0xdc,
   0xdd, 0xdd, 0xde, 0xde, 0xdf, 0xdf, 0xe0, 0xe0,
   0xe1, 0xe2, 0xe2, 0xe3, 0xe3, 0xe4, 0xe4, 0xe5,
   0xe5, 0xe6, 0xe6, 0xe7, 0xe7, 0xe8, 0xe8, 0xe9,
   0xe9, 0xea, 0xea, 0xeb, 0xeb, 0xec, 0xec, 0xed,
   0xed, 0xed, 0xee, 0xee, 0xef, 0xef, 0xf0, 0xf0,
   0xf1, 0xf1, 0xf2, 0xf2, 0xf3, 0xf3, 0xf4, 0xf4,
   0xf5, 0xf5, 0xf6, 0xf6, 0xf6, 0xf7, 0xf7, 0xf8,
   0xf8, 0xf9, 0xf9, 0xfa, 0xfa, 0xfb, 0xfb, 0xfb,
   0xfc, 0xfc, 0xfd, 0xfd, 0xfe, 0xfe, 0xff, 0xff };

/* todo: make this table smaller */
const uint32_t COLOR_RECIP[256] = {
   0x00000000, 0x00ff0000, 0x007f8000, 0x00550000,
   0x003fc000, 0x00330000, 0x002a8000, 0x00246db7,
   0x001fe000, 0x001c5555, 0x00198000, 0x00172e8c,
   0x00154000, 0x00139d8a, 0x001236db, 0x00110000,
   0x000ff000, 0x000f0000, 0x000e2aab, 0x000d6bca,
   0x000cc000, 0x000c2492, 0x000b9746, 0x000b1643,
   0x000aa000, 0x000a3333, 0x0009cec5, 0x000971c7,
   0x00091b6e, 0x0008cb09, 0x00088000, 0x000839ce,
   0x0007f800, 0x0007ba2f, 0x00078000, 0x00074925,
   0x00071555, 0x0006e453, 0x0006b5e5, 0x000689d9,
   0x00066000, 0x00063832, 0x00061249, 0x0005ee24,
   0x0005cba3, 0x0005aaab, 0x00058b21, 0x00056cf0,
   0x00055000, 0x0005343f, 0x0005199a, 0x00050000,
   0x0004e762, 0x0004cfb3, 0x0004b8e4, 0x0004a2e9,
   0x00048db7, 0x00047943, 0x00046584, 0x00045271,
   0x00044000, 0x00042e2a, 0x00041ce7, 0x00040c31,
   0x0003fc00, 0x0003ec4f, 0x0003dd17, 0x0003ce54,
   0x0003c000, 0x0003b216, 0x0003a492, 0x00039770,
   0x00038aab, 0x00037e3f, 0x0003722a, 0x00036666,
   0x00035af3, 0x00034fcb, 0x000344ec, 0x00033a54,
   0x00033000, 0x000325ed, 0x00031c19, 0x00031282,
   0x00030925, 0x00030000, 0x0002f712, 0x0002ee58,
   0x0002e5d1, 0x0002dd7c, 0x0002d555, 0x0002cd5d,
   0x0002c591, 0x0002bdef, 0x0002b678, 0x0002af28,
   0x0002a800, 0x0002a0fd, 0x00029a1f, 0x00029365,
   0x00028ccd, 0x00028656, 0x00028000, 0x000279c9,
   0x000273b1, 0x00026db7, 0x000267d9, 0x00026218,
   0x00025c72, 0x000256e6, 0x00025174, 0x00024c1c,
   0x000246db, 0x000241b3, 0x00023ca2, 0x000237a7,
   0x000232c2, 0x00022df3, 0x00022938, 0x00022492,
   0x00022000, 0x00021b81, 0x00021715, 0x000212bb,
   0x00020e74, 0x00020a3d, 0x00020618, 0x00020204,
   0x0001fe00, 0x0001fa0c, 0x0001f627, 0x0001f252,
   0x0001ee8c, 0x0001ead4, 0x0001e72a, 0x0001e38e,
   0x0001e000, 0x0001dc7f, 0x0001d90b, 0x0001d5a4,
   0x0001d249, 0x0001cefb, 0x0001cbb8, 0x0001c881,
   0x0001c555, 0x0001c235, 0x0001bf20, 0x0001bc15,
   0x0001b915, 0x0001b61f, 0x0001b333, 0x0001b051,
   0x0001ad79, 0x0001aaab, 0x0001a7e5, 0x0001a529,
   0x0001a276, 0x00019fcc, 0x00019d2a, 0x00019a91,
   0x00019800, 0x00019577, 0x000192f7, 0x0001907e,
   0x00018e0c, 0x00018ba3, 0x00018941, 0x000186e6,
   0x00018492, 0x00018246, 0x00018000, 0x00017dc1,
   0x00017b89, 0x00017957, 0x0001772c, 0x00017507,
   0x000172e9, 0x000170d0, 0x00016ebe, 0x00016cb1,
   0x00016aab, 0x000168aa, 0x000166ae, 0x000164b9,
   0x000162c8, 0x000160dd, 0x00015ef8, 0x00015d17,
   0x00015b3c, 0x00015966, 0x00015794, 0x000155c8,
   0x00015400, 0x0001523d, 0x0001507f, 0x00014ec5,
   0x00014d10, 0x00014b5f, 0x000149b2, 0x0001480a,
   0x00014666, 0x000144c7, 0x0001432b, 0x00014194,
   0x00014000, 0x00013e70, 0x00013ce5, 0x00013b5d,
   0x000139d9, 0x00013858, 0x000136db, 0x00013562,
   0x000133ed, 0x0001327b, 0x0001310c, 0x00012fa1,
   0x00012e39, 0x00012cd4, 0x00012b73, 0x00012a15,
   0x000128ba, 0x00012762, 0x0001260e, 0x000124bc,
   0x0001236e, 0x00012222, 0x000120d9, 0x00011f94,
   0x00011e51, 0x00011d11, 0x00011bd3, 0x00011a99,
   0x00011961, 0x0001182c, 0x000116f9, 0x000115ca,
   0x0001149c, 0x00011371, 0x00011249, 0x00011123,
   0x00011000, 0x00010edf, 0x00010dc1, 0x00010ca4,
   0x00010b8a, 0x00010a73, 0x0001095e, 0x0001084b,
   0x0001073a, 0x0001062b, 0x0001051f, 0x00010414,
   0x0001030c, 0x00010206, 0x00010102, 0x00010000 };

uint32_t khrn_color_rgba_pre(uint32_t rgba)
{
   uint32_t a = rgba >> 24;
   #define MUL(X, Y) ((((X) * (Y)) + (((X) * (Y)) >> 8) + (1 << 7)) >> 8)
   return
      (MUL((rgba >> 0) & 0xff, a) << 0) |
      (MUL((rgba >> 8) & 0xff, a) << 8) |
      (MUL((rgba >> 16) & 0xff, a) << 16) |
      (a << 24);
   #undef MUL
}

uint32_t khrn_color_rgba_unpre(uint32_t rgba)
{
   uint32_t a = rgba >> 24;
   uint32_t oo_a = (a == 0) ? 0 : float_to_int_shift(recip_((float)a * (1.0f / 255.0f)), 16);
   #define MUL(X, Y) (_min((((X) * (Y)) + (1 << 15)) >> 16, 255))
   return
      (MUL((rgba >> 0) & 0xff, oo_a) << 0) |
      (MUL((rgba >> 8) & 0xff, oo_a) << 8) |
      (MUL((rgba >> 16) & 0xff, oo_a) << 16) |
      (a << 24);
   #undef MUL
}

uint32_t khrn_color_rgba_rz(uint32_t rgba)
{
   return ((rgba >> 24) == 0) ? 0 : rgba;
}

uint32_t khrn_color_rgba_clamp_to_a(uint32_t rgba)
{
   uint32_t a = rgba >> 24;
   return
      (_min((rgba >> 0) & 0xff, a) << 0) |
      (_min((rgba >> 8) & 0xff, a) << 8) |
      (_min((rgba >> 16) & 0xff, a) << 16) |
      (a << 24);
}

uint32_t khrn_color_rgba_s_to_lin(uint32_t rgba)
{
   return
      ((uint32_t)COLOR_S_TO_LIN[(rgba >> 0) & 0xff] << 0) |
      ((uint32_t)COLOR_S_TO_LIN[(rgba >> 8) & 0xff] << 8) |
      ((uint32_t)COLOR_S_TO_LIN[(rgba >> 16) & 0xff] << 16) |
      (rgba & 0xff000000);
}

uint32_t khrn_color_rgba_lin_to_s(uint32_t rgba)
{
   return
      ((uint32_t)COLOR_LIN_TO_S[(rgba >> 0) & 0xff] << 0) |
      ((uint32_t)COLOR_LIN_TO_S[(rgba >> 8) & 0xff] << 8) |
      ((uint32_t)COLOR_LIN_TO_S[(rgba >> 16) & 0xff] << 16) |
      (rgba & 0xff000000);
}

uint32_t khrn_color_rgba_to_la_lin(uint32_t rgba)
{
   /*
      l = 0.2126 r + 0.7152 g + 0.0722 b
   */

   uint32_t l =
      ((3566836 * ((rgba >> 0) & 0xff)) +
      (11999065 * ((rgba >> 8) & 0xff)) +
      (1211315 * ((rgba >> 16) & 0xff)) +
      (1 << 23)) >> 24;
   return (l * 0x00010101) | (rgba & 0xff000000);
}

uint32_t khrn_color_rgba_to_la_s(uint32_t rgba)
{
   /*
      l = 0.2126 r + 0.7152 g + 0.0722 b
   */

   uint32_t l =
      ((13933 * (uint32_t)COLOR_S_TO_LIN16[(rgba >> 0) & 0xff]) +
      (46871 * (uint32_t)COLOR_S_TO_LIN16[(rgba >> 8) & 0xff]) +
      (4732 * (uint32_t)COLOR_S_TO_LIN16[(rgba >> 16) & 0xff]) +
      (1 << 15)) >> 16;
   uint32_t msb = clampi(_msb(l), 8, 12);
   l = COLOR_LIN16_TO_S[_min(
      /* offset of n-bit lookup table */
      ((msb - 8) << 4) +
      /* convert to n-bit (with rounding) */
      ((l + (1 << (msb - 5))) >> (msb - 4)),
      /* might get 320 due to rounding */
      319)];
   return (l * 0x00010101) | (rgba & 0xff000000);
}

static INLINE uint32_t clamp(int x)
{
   return _min(255, _max(0, x));
}

uint32_t khrn_color_rgba_add_dither(uint32_t rgba, int r, int g, int b, int a)
{
   return
      clamp(((rgba >> 0)  & 0xff) + r) << 0  |
      clamp(((rgba >> 8)  & 0xff) + g) << 8  |
      clamp(((rgba >> 16) & 0xff) + b) << 16 |
      clamp(((rgba >> 24) & 0xff) + a) << 24;
}

uint32_t khrn_color_rgba_transform(uint32_t rgba, const float *color_transform)
{
   float color[4];
   khrn_color_rgba_to_floats(color, rgba);
   return color_floats_to_rgba(
      (color[0] * color_transform[0]) + color_transform[4],
      (color[1] * color_transform[1]) + color_transform[5],
      (color[2] * color_transform[2]) + color_transform[6],
      (color[3] * color_transform[3]) + color_transform[7]);
}
